
// Global state
var scrapbook_all = {};
var scrapbook_all_raw = {};
var scrapbook_counter = 1;
var scrapbook_focus = null;
var scrapbook_name = "scrapbook"

Scrapbook = function() {};

Scrapbook.prototype = {

    startup: function() {
      // Listen in
      Windows.addObserver(this);
      // Sync with database { sends anything made in a previous session to us }
      DatabaseGet(this, scrapbook_name );
    },

    //////////////////////////////////////////////////////////////////////////
    // window events from lower level windowing system
    //////////////////////////////////////////////////////////////////////////

    onEndMove: function(name,win) {
      var obj = win.associated_object;
      if(obj && win && win.element && win.element.style) {
        obj.top = "" + parseInt(win.element.style.top);
        obj.left = "" + parseInt(win.element.style.left);
        obj.width = "" + parseInt(win.width);
        obj.height = "" + parseInt(win.height);
        DatabaseStore(this,scrapbook_name+"/"+obj.uid,obj);
      }
    },

    onEndResize: function(name,win) {
      var obj = win.associated_object;
      if(obj && win && win.element && win.element.style) {
        obj.top = "" + parseInt(win.element.style.top);
        obj.left = "" + parseInt(win.element.style.left);
        obj.width = "" + parseInt(win.width);
        obj.height = "" + parseInt(win.height);
        DatabaseStore(this,scrapbook_name+"/"+obj.uid,obj);
      }
    },

    onClose: function(name,win) {
      var obj = win.associated_object;
      DatabaseDelete(scrapbook_name+"/"+obj.uid);
    },

    // onDestroy()
    // onMinimize()
    // onMaximize()
    // onHide()
    // onShow()
    // onFocus()

    //////////////////////////////////////////////////////////////////////////
    // make a window
    //////////////////////////////////////////////////////////////////////////

    database_update: function(obj) {
    
      // Our soon to be window handle
      var win = null;
    
      // If no parent ignore ( the root of the tree is sent along sometimes )
      if(!obj.width) {
        return;
      }
    
      // for debugging only; grant an id if none
      if (!obj.uid) { obj.uid = "w_" + scrapbook_counter; scrapbook_counter++; }
      var id = obj.uid;
    
      // Modify an existing window?  
      if(!win && obj.uid) {
        win = scrapbook_all[id];
        // xxx move it only etcetera.
      }
    
      // Store raw obj too for good luck
      scrapbook_all_raw[id] = obj;
 
      // Build a new window?
      if(!win) {
 
        // just for fun - set a style if not set
        if(!obj.className) { obj.className = "mac_os_x" }
   
 obj.minimizable = false;
 obj.maximizable = false;

 
        // Make a new window
        scrapbook_all[id] = win = new Window(id,obj);
    
        // Remember raw object handle used to make it
        win.associated_object = obj;
    
        // A text editor is built in; attach it if it is requested
        if( obj.editor ) {
          var c = win.getContent;
       	  c.innerHTML = "<div id='window_body_"+id+"'\n\
       	                 onclick='WindowEdit(this);'\n\
                         style='width:100%;height:100%;text-align:left;cursor:pointer;'>\n\
                         "+obj.editor+"</div>";
        }
    
        // attach to a specified div? xxx this is not by name so it does not work in client/server model
        if(obj.attach) {
       	  win.setContent(obj.attach,true,true);
       	}
    
        // overflow
        if(obj.overflow) {
      	  win.getContent().setStyle({overflow: obj.overflow});
      	}
    
        // show it
      	win.show();
    
        // xxx It would be nice to load (and evaluate) any specified documents
        if(obj.load) {
        }
    
        // call any embedded boot logic last; returning any handler...
        if(obj.logic) {
          var logic=eval(obj.logic);
          if(logic) {
            // save a handle on the bootstrap logic - just for fun
            win.associated_logic = logic;
            obj.associated_logic = logic;
            // build the handler logic ; a class that manages this object
            var handler = logic(win);
            // save a handle on the handler itself; the real brains of this window
            win.associated_handler = handler;
            obj.associated_handler = handler;
          } else {
            alert("error parsing logic");
          }
        }
        return win;
      }
    },

    database_store: function(win) {
      DatabaseStore(this,scrapbook_name+"/#",win);
    }
};


//////////////////////////////////////////////////////////////////////////////
// inter client side widget communication; to allow apps to be decoupled
// a way to send stuff to all associated logic - stops when one returns not null
// not used atm
//////////////////////////////////////////////////////////////////////////////

function WindowMessage(event) {
  var result = null;
  for(var i in scrapbook_all) {
    if(scrapbook_all[i].associated_logic 
         && windows[i].associated_logic.message ) {
      result = windows[i].associated_logic.message(event);
      if (result ) break;
    }
  }
  return result;
}

/////////////////////////////////////////////////////////////////////////////
// testing some ideas for text editing in a hardcoded way - not super great
/////////////////////////////////////////////////////////////////////////////

var scrapbook_editor;

function WindowTextSave(contents) {
}

function WindowEdit(region) {
  // dojo text editor - very slow so turned off
  //editor = dojo.widget.createWidget("myEditor2",{},region);
  // a simpler text editor - faster
  // xxx needs love
  scrapbook_editor = new widgEditor();
  scrapbook_editor.initialize(region,WindowTextSave);
}

//////////////////////////////////////////////////////////////////////////////
// global convenience utilities
//////////////////////////////////////////////////////////////////////////////

var scrapbook = null;

// make a window locally only { we pretend message was sent to us from server }
function ScrapbookLocal(win) {
  scrapbook.database_update(win);
}

// make a window via server { sends message to server who returns it later }
function ScrapbookServer(win) {
  scrapbook.database_store(win);
}

// Boot windowing logic now; only one per page so is just global variable.
function ScrapbookStart() {
  scrapbook = new Scrapbook()
  scrapbook.startup();
}
